package cn.jji8.patternLogin.spigot.login;

import cn.jja8.patronSaint_2021_7_8_2056.bukkit.vision.StraightLine;
import cn.jji8.patternLogin.spigot.PatternLogin;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.scheduler.BukkitTask;

import java.util.ArrayList;
import java.util.List;

public abstract class LoginMode {
    //登入的玩家
    final Player player;
    //玩家选择的点
    List<Choice> spotList = new ArrayList<>();
    //玩家上次右键的时间
    long time = 0;
    //扫描玩家一直长按的任务
    BukkitTask task;
    @SuppressWarnings("unused")
    public LoginMode(Player player) {
        this.player = player;
    }
    /**
     * 位置的枚举
     * */
    @SuppressWarnings("unused")
    public enum Choice {
        A1,A2,A3,
        B1,B2,B3,
        C1,C2,C3;
        /**
         * 获得某点
         * */
        public static Choice gainChoice(int x,int y,int z){
            Location relative = new Location(LoginWorld.getLoginWord(),Configuring.LoginLocationX,Configuring.LoginLocationY,Configuring.LoginLocationZ);
            if(z!=relative.getBlockZ()+1){
                return null;
            }
            if(y==relative.getBlockY()+3){
                if(x==relative.getBlockX()+1){ return A1; }
                if(x==relative.getBlockX()+2){ return A2; }
                if(x==relative.getBlockX()+3){ return A3; }
                return null;
            }
            if(y==relative.getBlockY()+2){
                if(x==relative.getBlockX()+1){ return B1; }
                if(x==relative.getBlockX()+2){ return B2; }
                if(x==relative.getBlockX()+3){ return B3; }
                return null;
            }
            if(y==relative.getBlockY()+1){
                if(x==relative.getBlockX()+1){ return C1; }
                if(x==relative.getBlockX()+2){ return C2; }
                if(x==relative.getBlockX()+3){ return C3; }
                return null;
            }
            return null;
        }
        /**
         * 获得某点的坐标
         * */
        Location getLocation(){
            Location relative = new Location(LoginWorld.getLoginWord(),Configuring.LoginLocationX,Configuring.LoginLocationY,Configuring.LoginLocationZ);
            switch (this){
                case A1:return new Location(relative.getWorld(),relative.getBlockX()+1,relative.getBlockY()+3,relative.getBlockZ()+1);
                case A2:return new Location(relative.getWorld(),relative.getBlockX()+2,relative.getBlockY()+3,relative.getBlockZ()+1);
                case A3:return new Location(relative.getWorld(),relative.getBlockX()+3,relative.getBlockY()+3,relative.getBlockZ()+1);

                case B1:return new Location(relative.getWorld(),relative.getBlockX()+1,relative.getBlockY()+2,relative.getBlockZ()+1);
                case B2:return new Location(relative.getWorld(),relative.getBlockX()+2,relative.getBlockY()+2,relative.getBlockZ()+1);
                case B3:return new Location(relative.getWorld(),relative.getBlockX()+3,relative.getBlockY()+2,relative.getBlockZ()+1);

                case C1:return new Location(relative.getWorld(),relative.getBlockX()+1,relative.getBlockY()+1,relative.getBlockZ()+1);
                case C2:return new Location(relative.getWorld(),relative.getBlockX()+2,relative.getBlockY()+1,relative.getBlockZ()+1);
                case C3:return new Location(relative.getWorld(),relative.getBlockX()+3,relative.getBlockY()+1,relative.getBlockZ()+1);
            }
            System.out.println("出现问题？");
            return null;
        }
    }
    /**
     * 将List{Choice}序列化的方法，单向
     * */
    String choiceListToString(List<Choice> list){
        StringBuilder stringBuilder = new StringBuilder();
        for (Choice choice : list) {
            stringBuilder.append(choice.toString());
        }
        return stringBuilder.toString();
    }
    /**
     * 玩家右键
     * */
    void RightClick(PlayerInteractEvent a){
        time = System.currentTimeMillis();
        if(task==null){
            task = Bukkit.getServer().getScheduler().runTaskLaterAsynchronously(PatternLogin.getMain(), new Runnable() {
                @Override
                public void run() {
                    if (System.currentTimeMillis() - time < 2000) {
                        task=Bukkit.getServer().getScheduler().runTaskLaterAsynchronously(PatternLogin.getMain(), this, Configuring.time);
                        display();
                        return;
                    }
                    task=null;//这里代表玩家长按结束
                    if(spotList.size()<4){
                        LoginNews.news(player,"§a§l提示:§d请快一点点击，至少连接4个点。",80);
                    }else {
                        complete(spotList);
                    }
                    spotList = new ArrayList<>();
                }
            }, 5);
        }
        Block block = a.getClickedBlock();
        if(block==null){
            return;
        }
        Location location = block.getLocation();
        Choice choice = Choice.gainChoice(location.getBlockX(),location.getBlockY(),location.getBlockZ());
        if(choice==null){
            return;
        }
        if(!spotList.contains(choice)){
            spotList.add(choice);
            display();
        }
    }
    /**
     * 给玩家显示他的密码
     * */
    public void display(){
        Choice chronicle=null;
        for (Choice choice : spotList) {
            if(chronicle==null){
                chronicle=choice;
                continue;
            }
            Location l1 = chronicle.getLocation();
            Location l2 = choice.getLocation();
            if(l1==null|l2==null){
                continue;
            }
            StraightLine.straightLine(l1.add(0.5,0.5,0.5),l2.add(0.5,0.5,0.5),player,Configuring.density);
            chronicle=choice;
        }
    }
    /**
     * 玩家连接完成后调用此方法，其他实现类应该重写。
     * */
    abstract void complete(List<Choice> spotList);
}
